﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.Runtime.Serialization;

namespace Database
{

    [DataContract]
    public class SubForum
    {
        [DataMember]
        private int id;
        [DataMember]
        private string title;
       // [DataMember]
       // private User user;
        [DataMember]
        private SortedDictionary<int, Thread> threads;
        [DataMember]
        private SortedDictionary<string, User> moderators;

        //Complaints - tlunot
        [DataMember]
        private SortedDictionary<string, int> complaints;
        [DataMember]
        private int forumId;
        [DataMember]
        private int idForThreads;

        //User admin - forum's administrator. will be also moderator 
        public SubForum(int id, string title, int forumId, User admin) {
            this.id = id;
            this.title = title;
            this.threads = new SortedDictionary <int, Thread>();
            this.moderators = new SortedDictionary<string, User>();
            this.complaints = new SortedDictionary<string, int>();
            this.forumId = forumId;
            moderators.Add(admin.getUserName(), admin);
            idForThreads = 0;
        }

        public void setIdForThreads(int num) {
            this.idForThreads = num;
        }

        public int getId() {
            return this.id;
        }

        public string getTitle() {
            return this.title;
        }

        public void setTitle(string title) {
            this.title = title;
        }

        public SortedDictionary<int, Thread> getThreads()
        {
            return this.threads;
        }

        public void setThreads(SortedDictionary<int, Thread> threads)
        {
            this.threads = threads;
        }

        public SortedDictionary<string, User> getModerators() {
            return this.moderators;
        }
        public void setModerators(SortedDictionary<string, User> moderators)
        {
            this.moderators = moderators;
        }

        public SortedDictionary<string, int> getComplaints() {
            return this.complaints;
        }

        public void setComplaints(SortedDictionary<string, int> complaints)
        {
            this.complaints = complaints;
        }

        public int getForumId()
        {
            return this.forumId;
        }
        
        //return id of the new thread
        public int addThread(string title, string body, User user) {
            idForThreads += 1;
            Thread thread = new Thread(idForThreads, title, body, user, forumId, id);
            threads.Add(idForThreads, thread);
            return idForThreads;

        }

        public Thread getThread(int threadId) {
            if (threads.ContainsKey(threadId))
                return threads[threadId];
            else
                return null;
        }

        public bool removeThread(int threadId) {

            return threads.Remove(threadId); ;
        }

        //return true if the new moderator was added, otherwise false.
        public bool addModerator(User moderator) {
            bool ans = false;
            if (moderator != null && moderators.ContainsKey(moderator.getUserName()) == false){
                moderators.Add(moderator.getUserName(), moderator);
                ans = true;
            }
            return ans;
        }

        public bool removeModerator(User moderator){
            bool ans = false;
            if (moderator != null) {
                ans = moderators.Remove(moderator.getUserName());
                if (complaints.ContainsKey(moderator.getUserName()))
                    complaints.Remove(moderator.getUserName());
            }
            return ans;
        }

        public bool removeModerator(string moderator)
        {
            bool ans = false;
            if (moderator != null)
            {
                ans = moderators.Remove(moderator);
                if (complaints.ContainsKey(moderator))
                    complaints.Remove(moderator);
            }
            return ans;
        }

        public bool isModerator(User moderator) {
            bool ans = false;
            if (moderator != null)
                ans = moderators.ContainsKey(moderator.getUserName());
            return ans;
        }

        public bool isModerator(string moderator)
        {
            bool ans = false;
            if (moderator != null)
                ans = moderators.ContainsKey(moderator);
            return ans;
        }

        public User getModerator(string uname)
        {
            if (moderators.ContainsKey(uname))
                return moderators[uname];
            else
                return null;
        }

        public int getNumThreads() {
            return this.threads.Count;
        }

  /*      //Romove the moderator if the number of complaints is equal to 5, otherwise add 1 to number of complaints. 
        public void complain(User moderator) {
            if (moderator != null){
                if (moderators.ContainsKey(moderator.getUserName())){
                    if (complaints.ContainsKey(moderator.getUserName()))
                    {
                        int complaint = complaints[moderator.getUserName()] + 1;
                        if (complaint > 5)
                        {
                            moderators.Remove(moderator.getUserName());
                            complaints.Remove(moderator.getUserName());
                        }
                        else
                        {
                            if (!complaints.ContainsKey(moderator.getUserName()))
                                complaints.Add(moderator.getUserName(), complaint);
                            else
                            {
                                complaints.Remove(moderator.getUserName());
                                complaints.Add(moderator.getUserName(), complaint);
                            }
                        }
                    }
                    else
                        complaints.Add(moderator.getUserName(), 1);
                    }
            }
        }
       
   */ 
 
        public int getModeratorComplaints(User moderator) {
            if (moderator != null && complaints.ContainsKey(moderator.getUserName()))
                return complaints[moderator.getUserName()];
            else 
                return 0;        
        }

        public int getModeratorComplaints(string moderator)
        {
            if (moderator != null && complaints.ContainsKey(moderator))
                return complaints[moderator];
            else
                return 0;
        }

        public void setModeratorComplaints(string uname, int complaint) {
            if (uname != null)
            {
                if (moderators.ContainsKey(uname))
                {
                    if (complaints.ContainsKey(uname))
                    {
                       complaints.Remove(uname);
                       complaints.Add(uname, complaint);
                    }                     
                    else
                        complaints.Add(uname, complaint);
                }
            }
        }
        //return string of subforum moderators.
        public string SFModeratorsToString()
        {  
                string moderators = "";
                foreach (string uname in this.moderators.Keys)
                {
                    moderators += this.moderators[uname].getUserName() + ", ";
                }
                string NewModerators = moderators.Substring(0, moderators.Length - 2) + ".";
                return NewModerators;         
        }

        //return SortedDictionary <int, string>  int - id, string - thread title + body + posted user -> "title: body [user]"

        public SortedDictionary<int, string> subForumToString()
        { 
                SortedDictionary<int, string> titles = new SortedDictionary<int, string>();
                foreach (int id in this.threads.Keys)
                {
                    string title = this.threads[id].getTitle();
                    string body = this.threads[id].getBody();
                    string uname = this.threads[id].getUser().getUserName();
                    titles.Add(id, (title + ": " + body + ". [" + uname + "]"));
                }
                return titles;
        }

 
    }
}
